package com.caiwangdai.app.ui.home.borrow.list


import android.app.Activity
import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.layout.SingleLayoutHelper
import com.bigkoo.convenientbanner.ConvenientBanner
import com.bigkoo.convenientbanner.holder.Holder
import com.bigkoo.convenientbanner.listener.OnItemClickListener
import com.caiwangdai.app.R
import com.caiwangdai.app.bean.BannerB
import com.caiwangdai.app.bean.HomeContentB
import com.caiwangdai.app.extension.*
import com.caiwangdai.app.origin.list.holder.BaseRecyclerViewHolder
import com.caiwangdai.app.origin.web.NormalWebActivity
import kotlinx.android.synthetic.main.banner_item_home.view.*
import kotlinx.android.synthetic.main.vlayout_home_banner.view.*


class BannerAdapter(var mActivity: Activity?) : DelegateAdapter.Adapter<BaseRecyclerViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) = ViewHolder(parent?.inflate(R.layout.vlayout_home_banner))
    override fun onCreateLayoutHelper() = SingleLayoutHelper()
    override fun getItemCount() = 1

    val dataList = mutableListOf<BannerB>()

    override fun onBindViewHolder(holder: BaseRecyclerViewHolder?, position: Int) {
        val con = ((holder as ViewHolder).itemView.banner_ad) as ConvenientBanner<BannerB>
        //用于刷新banner使用
        con.setPageIndicator(intArrayOf(R.drawable.ic_indicator, R.drawable.ic_indicator_focus))
        con.setPages({ BannerHolderView() }, dataList)
        con.startTurning(4000)

        con.setOnItemClickListenerPro {
            val item = dataList[it]
            mActivity?.startActivity(NormalWebActivity.newInstance(mActivity!!, item.link_url!!))
        }
    }

    fun setDataToAdapter(dataList: List<BannerB>) {
        this.dataList.clear()
        this.dataList.addAll(dataList)
        notifyDataSetChanged()
    }


    open class ViewHolder(itemView: View?) : BaseRecyclerViewHolder(itemView)

    open class BannerHolderView : Holder<BannerB> {
        var image: ImageView? = null

        override fun createView(context: Context?): View? {
            val View = LayoutInflater.from(context).inflate(R.layout.banner_item_home, null)
            image = View.iv_ad
            return View
        }

        override fun UpdateUI(context: Context?, position: Int, data: BannerB?) {
            image?.loadUrl(data?.image_url!!)
        }
    }

}

//增加的扩展方法
private var lastClickTime: Long = 0

fun ConvenientBanner<BannerB>.setOnItemClickListenerPro(singleClick: ((position: Int) -> Unit)) {
    this.setOnItemClickListener {
        val start = System.currentTimeMillis()
        val diff = start - lastClickTime
        if (diff in 1..700) {
            return@setOnItemClickListener
        }
        lastClickTime = start
        singleClick.invoke(it)
    }
}